/*
 *  Copyright (c) 2023. felord.cn
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *       https://www.apache.org/licenses/LICENSE-2.0
 *  Website:
 *       https://felord.cn
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.hhkj.study.common.callback;

import cn.felord.callbacks.CallbackEventBody;
import cn.felord.enumeration.CallbackChangeType;
import cn.felord.enumeration.CallbackEvent;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.study.admin.user.entity.User;
import com.hhkj.study.admin.user.model.podo.UserWechatCompanyDo;
import com.hhkj.study.admin.user.service.base.UserService;
import com.hhkj.study.admin.user.service.extend.UserWechatCompanyExtendService;
import com.hhkj.study.common.model.req.WechatQrState;
import com.ruoyi.common.base.biz.service.callback.CallbackEventBodyConsumer;
import com.ruoyi.common.cont.SysConst;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 客户变更事件业务处理
 *
 * @author dax
 * @since 2023/5/30 13:42
 */
@Slf4j
@Service
@AllArgsConstructor
public class ChangeExternalContactCallbackEventBodyConsumer implements CallbackEventBodyConsumer {

    @Resource
    private UserWechatCompanyExtendService userWechatCompanyExtendService;

    @Resource
    private UserService userService;

    @Override
    public boolean matched(CallbackEvent event) {
        return CallbackEvent.CHANGE_EXTERNAL_CONTACT.equals(event);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void consume(CallbackEventBody body) {
        log.info("客户变更事件:用户二维码参数{},被添加的企业微信id:{}", body.getState(), body.getUserId());
        // 用户二维码中参数
        WechatQrState qrState = JSONUtil.toBean(body.getState(), WechatQrState.class);
        // 被添加的企业用户id
        String userId = body.getUserId();
        CallbackChangeType changeType = body.getChangeType();
        // 幂等保存用户添加客服数据 修改用户添加客服标识
        if (Objects.equals(changeType, CallbackChangeType.ADD_EXTERNAL_CONTACT)) {
            // 新增用户添加客服
             UserWechatCompanyDo userWechatCompanyDo = new UserWechatCompanyDo();
             userWechatCompanyDo.setAddSource(qrState.getAddSource());
             userWechatCompanyDo.setUserId(qrState.getUserId());
             userWechatCompanyDo.setWxOrgId(userId);
             userWechatCompanyExtendService.saveNoRepeat(userWechatCompanyDo);

             // 更新用户是否添加客服标识
            LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(User::getId, qrState.getUserId()).eq(User::getAddWxCompanyFlag, SysConst.Flag.NO.getFlag())
                    .set(User::getAddWxCompanyFlag, SysConst.Flag.IS.getFlag());
            userService.update(updateWrapper);
        }
    }
}
